package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.StaffEvaluationMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.StaffEvaluation;
import edu.hubu.talentmanagementsystem.service.IStaffEvaluationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class StaffEvaluationServiceImpl extends ServiceImpl<StaffEvaluationMapper, StaffEvaluation> implements IStaffEvaluationService {

    private final MapperContext context;

    public StaffEvaluationServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getStaffEvaluationMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<StaffEvaluation> insert(@EncodeParam StaffEvaluation record) {
        int insert = context.getStaffEvaluationMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getStaffEvaluationMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<StaffEvaluation> selectById(Integer id) {
        QueryWrapper<StaffEvaluation> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<StaffEvaluation> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(StaffEvaluation::getStaffEvaluationId, id);
        return Optional.of(context.getStaffEvaluationMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<StaffEvaluation> selectAll(String order) {
        QueryWrapper<StaffEvaluation> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<StaffEvaluation> select = getColumnWrapper(wrapper.lambda());
            return context.getStaffEvaluationMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<StaffEvaluation> select = getColumnWrapper(wrapper.lambda());
        return context.getStaffEvaluationMapper().selectList(select);
    }

    @Override
    public Page<StaffEvaluation> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<StaffEvaluation> wrapper = new QueryWrapper<>();
        Page<StaffEvaluation> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<StaffEvaluation> select = getColumnWrapper(wrapper.lambda());
            return context.getStaffEvaluationMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<StaffEvaluation> select = getColumnWrapper(wrapper.lambda());
        return context.getStaffEvaluationMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<StaffEvaluation> getColumnWrapper(LambdaQueryWrapper<StaffEvaluation> wrapper) {
        return wrapper.select(
                StaffEvaluation::getStaffArchivalHr
                    ,
                StaffEvaluation::getStaffEvaluationId
                    ,
                StaffEvaluation::getStaffArchivalCompanyId
                    ,
                StaffEvaluation::getStaffArchivalHrId
                    ,
                StaffEvaluation::getStaffEvaluationEntryDate
                    ,
                StaffEvaluation::getStaffEvaluationLeftDate
                    ,
                StaffEvaluation::getStaffEvaluationPerformance
                    ,
                StaffEvaluation::getStaffEvaluationPerformanceRemarks
                    ,
                StaffEvaluation::getCompany
        );
    }

    @Override
    public Page<StaffEvaluation> selectLikePaged( Integer currentPage,
             Integer pageSize, StaffEvaluation record, String order) {
        Page<StaffEvaluation> page = new Page<>(currentPage, pageSize);
        QueryWrapper<StaffEvaluation> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getStaffEvaluationMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getStaffEvaluationMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<StaffEvaluation> getQueryWrapper(QueryWrapper<StaffEvaluation>wrapper, StaffEvaluation record) {
        LambdaQueryWrapper<StaffEvaluation> lambda = wrapper.lambda();
        if (record.getStaffArchivalHr() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffArchivalHr,record.getStaffArchivalHr());
        }
        if (record.getStaffEvaluationId() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffEvaluationId,record.getStaffEvaluationId());
        }
        if (record.getStaffArchivalCompanyId() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffArchivalCompanyId,record.getStaffArchivalCompanyId());
        }
        if (record.getStaffArchivalHrId() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffArchivalHrId,record.getStaffArchivalHrId());
        }
        if (record.getStaffEvaluationEntryDate() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffEvaluationEntryDate,record.getStaffEvaluationEntryDate());
        }
        if (record.getStaffEvaluationLeftDate() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffEvaluationLeftDate,record.getStaffEvaluationLeftDate());
        }
        if (record.getStaffEvaluationPerformance() != null) {
            lambda = lambda.eq(StaffEvaluation::getStaffEvaluationPerformance,record.getStaffEvaluationPerformance());
        }
        if (record.getCompany() != null) {
            lambda = lambda.eq(StaffEvaluation::getCompany,record.getCompany());
        }
        if (record.getStaffEvaluationPerformanceRemarks() != null) {
            lambda = lambda.like(StaffEvaluation::getStaffEvaluationPerformanceRemarks,record.getStaffEvaluationPerformanceRemarks ());
        }
        return lambda;
    }

    public int count(StaffEvaluation record) {
        return context.getStaffEvaluationMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<StaffEvaluation> updateById(Integer id, @EncodeParam StaffEvaluation record) {
        record.setStaffEvaluationId(id);
        int update = context.getStaffEvaluationMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<StaffEvaluation> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<StaffEvaluation> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(StaffEvaluation::getStaffEvaluationId, id);
            return Optional.of(context.getStaffEvaluationMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }

    @Override
    public List<StaffEvaluation> selectByStaffId(Integer staffId) {
        return context.getStaffEvaluationMapper().selectByStaffId(staffId);
    }
}
